<template>
  <el-dialog
     width="600px"
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="用户密码" prop="password" :class="{ 'is-required': !dataForm.id }">
      <el-input v-model="dataForm.password" placeholder="用户密码" type="password"></el-input>
    </el-form-item>
    <el-form-item label="应用名称" prop="appName">
      <el-input v-model="dataForm.appName" placeholder="应用名称"></el-input>
    </el-form-item>
    <el-form-item label="所属单位" prop="company">
      <el-input v-model="dataForm.company" placeholder="所属单位"></el-input>
    </el-form-item>
    <el-row>
     <el-col :span="11">
      <el-form-item label="性别" prop="sex">
        <el-select v-model="dataForm.sex" placeholder="请选择">
            <el-option  v-for="(item) in sexs" :key="item.value" :label="item.label" :value="item.value"></el-option>
         </el-select>
       </el-form-item>
    </el-col>
  <el-col :span="8">
     <el-form-item label="生日"  prop="birthday" >
          <el-date-picker clearable size="medium"
            v-model="dataForm.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择生日"
            :picker-options="pickerOptions"
            >
          </el-date-picker>
    </el-form-item>
  </el-col>
</el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userId: '',
          userName: '',
          password: '',
          appName: '',
          sex: '',
          company: '',
          birthday: ''
        },
         sexs: [{
          value: '0',
          label: '男'
        }, {
          value: '1',
          label: '女'
        }],
        //禁用选择未来日期
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() >Date.now() 
        }
      },
        dataRule: {
          userName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '用户密码不能为空', trigger: 'blur' }
          ],
          appName: [
            { required: true, message: '应用名称不能为空', trigger: 'blur' }
          ],
          company: [
            { required: true, message: '所属单位不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          birthday: [
            { required: true, message: '生日不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/iotuser/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.iotUser.userId
                this.dataForm.userName = data.iotUser.userName
                this.dataForm.appName = data.iotUser.appName
                this.dataForm.sex = data.iotUser.sex
                this.dataForm.company = data.iotUser.company
                this.dataForm.birthday = data.iotUser.birthday
                this.dataForm.age = data.iotUser.age
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/iotuser/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'put',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'userName': this.dataForm.userName,
                'password': this.dataForm.password,
                'appName': this.dataForm.appName,
                'sex': this.dataForm.sex,
                'company': this.dataForm.company,
                'birthday': this.dataForm.birthday
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
